console.log(window.Tezero);
const THREE=Tezero.THREE;
const tz=new Tezero(document.querySelector('#container'),{precision:'lowp',mode:'edit'});

window.tz=tz;
tz.background='#cccccc';
tz.control={
  autoRotate:true,
};

tz.connectMqtt('ws://broker.emqx.io:8083/mqtt','tezero');

const mesh=tz.add({
    type:'box',
    width:1,
    height:1,
    position:new THREE.Vector3(0,0.5,0),
    animations:[{key:'position',to:{x:-10,z:6},time:3000,repeat:-1,start:true,yoyo:true},{key:'rotation',time:3000,to:{y:11},repeat:-1,start:true,yoyo:false}]
})
tz.add({
    type:'cylinder',
    radiusTop:0.5,
    radiusBottom:0.5,
    position:new THREE.Vector3(3,0.5,0),
    materialType:'Standard',
    material:{
        color:0x4444f4,
    }
})

// tz.add({
//     type:'plane',
//     width:10,
//     height:10,
//     materialType:'Standard',
//     material:{
//         color:0x333333,
//     }
// })
tz.add({
    type:'ambientLight',
    color:0xffffff,
    intensity:.7,
})

tz.add({
  type:'text2d',
  text:'文字\n这是第二行',
  padding:[6,2],
  borderWidth:0.5,
  borderRadius:3,
  borderColor:'lightgrey',
  color:'#fffccc',
  backgroundColor:'rgba(0,0,190,0.6)',
  position:{x:0,y:2,z:0},
})

const light=tz.add({
    type:'directionalLight',
    color:0xffffff,
    intensity:1,
    position:new THREE.Vector3(3,4,2)
})
light.target=mesh;
tz.select(null);

document.getElementById('play').onclick=e=>{
  if(e.target.innerText.trim()==='启动'){
    e.target.innerText='停止';
    tz.startPlayer();
  }else{
    e.target.innerText='启动';
    tz.stopPlayer();
  }
}
document.getElementById('open').onclick=e=>{
  const fileInput = document.createElement( 'input' );
  fileInput.multiple = false;
  fileInput.type = 'file';
  fileInput.accept=".view";
  fileInput.addEventListener( 'change', (e)=> {
    // console.log(e);
    let file = e.target.files[0];
 
    if (!file) {
        return;
    }
    
    let reader = new FileReader();
    
    reader.addEventListener('load', event => {
        let content = event.target.result;
        tz.open(new Uint8Array(content));
    });
    
    reader.readAsArrayBuffer(file);
  } );
  fileInput.click();
}
document.getElementById('import').onclick=e=>{
  tz.editor.importFiles();
}
document.getElementById('export').onclick=e=>{
  const result=tz.data;
   
  // 1. 创建一个 Blob 对象
  const blob = new Blob([result], { type: 'text/plain' });
  
  // 2. 创建一个 URL 对象
  const downloadUrl = URL.createObjectURL(blob);
  
  // 3. 创建一个 <a> 元素并设置下载链接和文件名
  const downloadLink = document.createElement('a');
  downloadLink.href = downloadUrl;
  downloadLink.download = 'tezero.view';
  
  // 4. 模拟用户点击下载链接
  downloadLink.click();
  
  // 5. 清除 URL 对象
  URL.revokeObjectURL(downloadUrl);
}

// const view=new Tezero.View(document.getElementById('container'));
// setTimeout(()=>{
//   view.open(tz.data);
// },1000);
